package com.sls.game.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.DataConversion;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.assist.Helper;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.GoodsOrderDAO;
import com.sls.game.dao.GoodsOrderLogDAO;
import com.sls.game.pojo.GoodsOrderInfo;
import com.sls.game.pojo.GoodsOrderLog;
import com.sls.game.pojo.NutsNetOrderRefundInfo;
import com.sls.game.service.NutsNetOrderRefundService;

/**
 * 
 * @description 交易订单退款通知接口
 * @author Wangxuegang
 * @date 2012-9-5
 * 
 */
@Service("nutsNetOrderRefundService")
public class NutsNetOrderRefundServiceImpl implements NutsNetOrderRefundService {
	private static Logger log = Logger
			.getLogger(NutsNetOrderRefundServiceImpl.class);

	private Gson gson = null;

	public NutsNetOrderRefundServiceImpl() {
		gson = CommonUtil.gson();
	}

	/**
	 * 网游充值订单表DAO
	 */
	private GoodsOrderDAO goodsOrderDAO;

	private GoodsOrderLogDAO goodsOrderLogDAO;

	public GoodsOrderLogDAO getGoodsOrderLogDAO() {
		return goodsOrderLogDAO;
	}

	@Resource(name = "goodsOrderDAO")
	public void setGoodsOrderLogDAO(GoodsOrderLogDAO goodsOrderLogDAO) {
		this.goodsOrderLogDAO = goodsOrderLogDAO;
	}

	public GoodsOrderDAO getGoodsOrderDAO() {
		return goodsOrderDAO;
	}

	@Resource(name = "goodsOrderDAO")
	public void setGoodsOrderDAO(GoodsOrderDAO goodsOrderDAO) {
		this.goodsOrderDAO = goodsOrderDAO;
	}

	/**
	 * 交易订单退款通知接口
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String insert(String body, Head head) throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		// 对消息body进行解析
		NutsNetOrderRefundInfo info = gson.fromJson(body,
				NutsNetOrderRefundInfo.class);
		if (info == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
		boolean flag = checkDataOfInsert(info);
		if (flag) { // 校验通过
			// 更新订单表
			updateGoodsorder(head, info);
		}

		return null;
	}

	/**
	 * 根据交易返回的结果更新订单表
	 * 
	 * @param head
	 * 
	 * @param info
	 * 
	 */
	private void updateGoodsorder(Head head, NutsNetOrderRefundInfo info) {
		// 合作方平台订单编号
		String ptOrderNo = info.getPtOrderNo();
		// SUP平台订单号
		String supOrderNo = info.getSupOrderNo();
		// SUP平台退款种类
		String reType = info.getReType();
		// 退款金额
		String rePrice = info.getRePrice();
		int value = DataConversion.converStringToInteger(rePrice);

		GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
		goodsOrderInfo.setOrderId(ptOrderNo);
		goodsOrderInfo.setSupOrderId(supOrderNo);
		goodsOrderInfo.setReType(reType);
		goodsOrderInfo.setRePrice(value);

		// 数据更新
		try {
			goodsOrderDAO.update(goodsOrderInfo);
		} catch (Exception e) {
			log.error(e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

		// 写订单日志
		try {
			String orderId = ptOrderNo;
			GoodsOrderInfo goodsOrderInfo1 = new GoodsOrderInfo();
			goodsOrderInfo1 = this.goodsOrderDAO.queryByOrderId(orderId);
			if (goodsOrderInfo1 != null) {
				GoodsOrderLog orderLog = Helper.copy(goodsOrderInfo1);
				orderLog.setId(UUID.randomUUID().toString());
				orderLog.setModifyTime(ServerDateAndTime
						.getNowDateTimeOfFormat());
				this.goodsOrderLogDAO.insert(orderLog);
			}

		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

	}

	/**
	 * 数据校验
	 * 
	 * @param info
	 */
	private boolean checkDataOfInsert(NutsNetOrderRefundInfo info) {
		boolean flag = false;
		// 合作平台编号
		String partnerId = info.getPartnerId();
		// 合作方平台订单编号
		String ptOrderNo = info.getPtOrderNo();
		// SUP平台订单号
		String supOrderNo = info.getSupOrderNo();
		// SUP平台退款种类
		String reType = info.getReType();
		// 退款金额
		String rePrice = info.getRePrice();
		// 签名字符串
		String sign = info.getSign();

		if ("".equals(partnerId) || partnerId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_PLATFORMNO_NULL.getCode(),
					ExceptionCodeDes.COOPERATION_PLATFORMNO_NULL
							.getDescription());
		} else if ("".equals(ptOrderNo) || ptOrderNo == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.COOPERATION_PLATFORM_ORDERNO_NULL
							.getCode(),
					ExceptionCodeDes.COOPERATION_PLATFORM_ORDERNO_NULL
							.getDescription());
		} else if ("".equals(supOrderNo) || supOrderNo == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SUP_ORDER_NO_NULL.getCode(),
					ExceptionCodeDes.SUP_ORDER_NO_NULL.getDescription());
		} else if ("".equals(reType) || reType == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.REFUND_TYPE_NULL.getCode(),
					ExceptionCodeDes.REFUND_TYPE_NULL.getDescription());
		} else if ("".equals(rePrice) || rePrice == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.REFUND_AMOUNT_NULL.getCode(),
					ExceptionCodeDes.REFUND_AMOUNT_NULL.getDescription());
		} else if ("".equals(sign) || sign == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SIG_NATURE_NULL.getCode(),
					ExceptionCodeDes.SIG_NATURE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

}
